<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('行业') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
      <el-form-item :label="$t('名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:industry:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sysIndustryList" row-key="id" lazy :load="load" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" border>
      <el-table-column :label="$t('名称')" prop="name" />
      <el-table-column :label="$t('排序')" align="center" prop="sort" width="150" />
      <el-table-column :label="$t('创建时间')" align="center" prop="createDate" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:industry:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['system:area:edit']"
            size="small"
            type="text"
            icon="el-icon-plus"
            @click="addHandle(scope.row)"
          >{{ $t("新增") }}</el-button>
          <el-button
            v-hasPermi="['system:industry:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改行业对话框 -->
    <el-dialog :title="$t('行业')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <sysIndustryForm ref="sysIndustryForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listSysIndustry, delSysIndustry, exportSysIndustry } from '@/api/system/industry/sysIndustry'
import sysIndustryForm from './sysIndustryForm.vue'
export default {
  components: {
      sysIndustryForm
  },
  data() {
    return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 行业表格数据
        sysIndustryList: [],
        // 是否显示弹出层
        formOpen: false,
        // 查询参数
        queryParams: {
            parentId: undefined,
            name: undefined,
            pageNum: 1,
            pageSize: 10
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        parentId: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], name: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], sort: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], createBy: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], createDate: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], updateBy: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], updateDate: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], delFlag: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ] }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询行业列表 */
    getList() {
        this.loading = true
      this.queryParams.parentId = '0'
        listSysIndustry(this.queryParams).then(response => {
            this.sysIndustryList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            parentId: undefined,
            name: undefined,
            sort: undefined,
            createBy: undefined,
            createDate: undefined,
            updateBy: undefined,
            updateDate: undefined,
            remarks: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle(row) {
      console.log(row)
        this.formOpen = true
        this.$nextTick(() => {
            if (row != null) {
              this.$refs.sysIndustryForm.init({ 'parentId': row.id, 'name': row.name })
            } else {
              this.$refs.sysIndustryForm.init(null)
            }
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.sysIndustryForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delSysIndustry(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportSysIndustry(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    load(tree, treeNode, resolve) {
      listSysIndustry({ 'parentId': tree.id }).then(response => {
        resolve(response.rows)
      })
    }
  }
}
</script>
